package cc.rboot.repeat.aspect;

import cc.rboot._core.holder.AppContextHolder;
import cc.rboot.repeat.annotation.RepeatCommitValidator;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
public class RepeatCommitAspect {

    @Pointcut("@annotation(cc.rboot.repeat.annotation.RepeatCommitValidator)")
    public void pointCut() {
    }

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method targetMethod = methodSignature.getMethod();

        RepeatCommitValidator repeatCommitValidator = targetMethod.getAnnotation(RepeatCommitValidator.class);

        if (AppContextHolder.getBean(repeatCommitValidator.validator()).validator()) {
            return joinPoint.proceed();
        }

        throw new RuntimeException("请勿重复提交");
    }
}
